﻿using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;
using System.Reflection;
using SSPharma.Model;
using System.Collections.ObjectModel;
//
using System.Configuration;
using System.Data;
using System.Windows;
using DBAccess;
using SSPharma.Common;
using SSPharma.View;
using SSPharma.ViewModel;
using System.Windows.Controls;
//


namespace SSPharma.ViewModel
{
    public class MenuItemViewModel : ViewModelBase
    {
        #region Fields

        readonly SSPharma.Model.MenuItem _menuItem;
        private bool _isSelected;
        private MainWindowViewModel _mainWindowViewModel;

        #endregion

        #region Constructor

        public MenuItemViewModel(SSPharma.Model.MenuItem menuItem, MainWindowViewModel mainWindowViewModel)
        {
            ChildMenuCollection = new ObservableCollection<MenuItemViewModel>();
            _menuItem = menuItem;
            _mainWindowViewModel = mainWindowViewModel;
        }

        #endregion

        #region MenuItem Properties

        public string MenuID
        {
            get { return _menuItem.MenuID; }
            set
            {
                if (value == _menuItem.MenuID)
                    return;

                _menuItem.MenuID = value;

                base.OnPropertyChanged("MenuID");
            }
        }

        public string ParentID
        {
            get { return _menuItem.ParentID; }
            set
            {
                if (value == _menuItem.ParentID)
                    return;

                _menuItem.ParentID = value;

                base.OnPropertyChanged("ParentID");
            }
        }

        public string MenuName
        {
            get { return _menuItem.MenuName; }
            set
            {
                if (value == _menuItem.MenuName)
                    return;

                _menuItem.MenuName = value;

                base.OnPropertyChanged("MenuName");
            }
        }

        public string IconPath
        {
            get { return _menuItem.IconPath; }
            set
            {
                if (value == _menuItem.IconPath)
                    return;

                _menuItem.IconPath = value;

                base.OnPropertyChanged("IconPath");
            }
        }

        public string FormName
        {
            get { return _menuItem.FormName; }
            set
            {
                if (value == _menuItem.FormName)
                    return;

                _menuItem.FormName = value;

                base.OnPropertyChanged("FormName");
            }
        }

        public string Views
        {
            get { return _menuItem.Views; }
            set
            {
                if (value == _menuItem.Views)
                    return;

                _menuItem.FormName = value;

                base.OnPropertyChanged("Views");
            }
        }

        public string ViewModel
        {
            get { return _menuItem.ViewModel; }
            set
            {
                if (value == _menuItem.ViewModel)
                    return;

                _menuItem.ViewModel = value;

                base.OnPropertyChanged("ViewModel");
            }
        }

        public string DirectoryViews
        {
            get { return _menuItem.DirectoryViews; }
            set
            {
                if (value == _menuItem.DirectoryViews)
                    return;

                _menuItem.DirectoryViews = value;

                base.OnPropertyChanged("DirectoryViews");
            }
        }

        public string DirectoryViewModel
        {
            get { return _menuItem.DirectoryViewModel; }
            set
            {
                if (value == _menuItem.DirectoryViewModel)
                    return;

                _menuItem.DirectoryViewModel = value;

                base.OnPropertyChanged("DirectoryViewModel");
            }
        }

        public bool IsForm
        {
            get { return _menuItem.IsForm; }
            set
            {
                if (value == _menuItem.IsForm)
                    return;

                _menuItem.IsForm = value;

                base.OnPropertyChanged("IsForm");
            }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected)
                    return;

                _isSelected = value;

                if (_isSelected)
                {
                    HienThiForm();
                }

                base.OnPropertyChanged("IsSelected");
            }
        }

        public ObservableCollection<MenuItemViewModel> ChildMenuCollection
        {
            get;
            set;
        }

       

        #endregion

        #region Public Methods

        public MenuItemViewModel GetMenuItemModelByID(string _menuID)
        {
            if (MenuID.Equals(_menuID))
            {
                return this;
            }
            else
            {
                foreach (var item in ChildMenuCollection)
                {
                    MenuItemViewModel menuItemModel = item.GetMenuItemModelByID(_menuID);
                    if (menuItemModel != null)
                    {
                        return menuItemModel;
                    }
                }
            }

            return null;

        }


        #endregion

        #region Private Methods

        private void HienThiForm()
        {
            if (IsForm && !string.IsNullOrEmpty(ViewModel) && !string.IsNullOrEmpty(ViewModel))
            {
                string formTypeFullName = string.Format("SSPharma.View.{0}.{1}", DirectoryViews, Views);
                Type type = Type.GetType(formTypeFullName, true);
                dynamic frmmethod = Activator.CreateInstance(type);
                dynamic Methos = Assembly.GetExecutingAssembly().CreateInstance(string.Format("SSPharma.ViewModel.{0}.{1}", DirectoryViewModel, ViewModel));
                frmmethod.DataContext = Methos;
                frmmethod.ShowDialog();
            }
            else if (!string.IsNullOrEmpty(ViewModel) && !string.IsNullOrEmpty(ViewModel))
            {
                UserControl uc = (UserControl)Assembly.GetExecutingAssembly().CreateInstance(string.Format("SSPharma.View.{0}.{1}", DirectoryViews, Views));
                uc.Name = Views;
                dynamic Methos = Assembly.GetExecutingAssembly().CreateInstance(string.Format("SSPharma.ViewModel.{0}.{1}", DirectoryViewModel, ViewModel));
                Methos.FormName = FormName;
                uc.DataContext = Methos;
                _mainWindowViewModel.CurrentMenuViewModel = uc;
                _mainWindowViewModel.CurrentMenuName = MenuName;
            }
            else
                _mainWindowViewModel.CurrentMenuViewModel = new SSPharma.View.HeThong.Desktop();
        }

        #endregion

    }
}